﻿using MxWeiXinPF.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;


namespace MxWeiXinPF.Templates
{
    public class ShopBasePage : System.Web.UI.Page
    {
         /// <summary>
        /// 模版的物理路径
        /// </summary>
        public string serverPath { get; set; }

        /// <summary>
        /// 模版的虚拟路径，比如/shop/templates/default
        /// </summary>
        public string tPath { get; set; }

        /// <summary>
        /// 模版文件名称
        /// </summary>
        public string templateFileName { get; set; }

        /// <summary>
        /// 微帐号wid
        /// </summary>
        public int wid { get; set; }

        public int shopid { get; set; }
        /// <summary>
        /// 初始化模版的错误信息
        /// </summary>
        public string errInitTemplates { get; set; }

        public ShopBasePage()
        {
            serverPath = "";
            templateFileName = "";
            errInitTemplates = "";
             wid =  MyCommFun.RequestInt("wid");
             shopid = MyCommFun.RequestInt("shopid");
             if (wid == 0)
            {
                errInitTemplates = "链接地址或者参数错误！";
                return;
            }
          
        }

        protected virtual void Page_Load(object sender, EventArgs e)
        {
 
        }
        protected MxWeiXinPF.Model.user_groups groupModel;
        protected MxWeiXinPF.Model.users userModel;

        public MxWeiXinPF.Model.users GetUserInfo()
        {
            if (this.IsUserLogin())
            {
                MxWeiXinPF.Model.users users = HttpContext.Current.Session["dt_session_user_info"] as MxWeiXinPF.Model.users;
                if (users != null)
                {
                    return new MxWeiXinPF.BLL.users().GetModel(users.id);
                }
            }
            return null;
        }

        public bool IsUserLogin()
        {
            if (HttpContext.Current.Session["dt_session_user_info"] != null)
            {
                string openid = MyCommFun.RequestOpenid();
                if (!string.IsNullOrEmpty(openid))
                {
                    Model.users modelByOpenID = new BLL.wx_dt_user_BaseData().GetModelByOpenID(openid);
                    if (modelByOpenID != null)
                    {
                        this.Session["dt_session_user_info"] = modelByOpenID;
                        this.Session.Timeout = 0x2d;
                    }
                }
                return true;
            }
            string cookie = Utils.GetCookie("dt_cookie_user_name_remember", "MxWeiXinPF");
            string password = Utils.GetCookie("dt_cookie_user_pwd_remember", "MxWeiXinPF");
            if ((cookie != "") && (password != ""))
            {
                MxWeiXinPF.Model.users users2 = new MxWeiXinPF.BLL.users().GetModel(cookie, password, 0, 0, false);
                if (users2 != null)
                {
                    HttpContext.Current.Session["dt_session_user_info"] = users2;
                    return true;
                }
            }
            return false;
        }
    }
}
